/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.lm.anotacoes;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

/**
 * @author Robson Motta
 */
public class ReadContextWebInf {
    
   @SuppressWarnings("CallToThreadDumpStack")
   public static String getContextWebInf(String parametroSevlet) {
        String contextPath = "";       
        String url = "/AnnotationServlet";    //http://localhost:8080/AnnotationServlet";
        try {
            //Abre a conexao com servlet
            URLConnection connection = new URL(url).openConnection();
            connection.setDoOutput(true);  //metodo POST;
            connection.setRequestProperty("Accept-Charset", "UTF-8");                
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + "UTF-8");

            //Envia dados para o servlet
            if (parametroSevlet == null) 
                parametroSevlet = "* Uso Livre *";
            DataOutputStream os = (DataOutputStream)connection.getOutputStream(); 
            os.writeUTF(parametroSevlet.trim());
            os.flush();
            os.close();
                 
            //Recebe a resposta do servlet
            DataInputStream is =(DataInputStream)connection.getInputStream();  
            int ch;
            StringBuilder sb = new StringBuilder();
            while ((ch = is.read()) != -1) {
               sb.append((char)ch);
            }
            contextPath = sb.toString(); 
            is.close();                
       }
       catch (MalformedURLException e) {     
            e.printStackTrace();     
       } catch (IOException e) {     
            e.printStackTrace();     
       } catch (Exception e) {     
            e.printStackTrace();     
       }   
       return contextPath; 
   } 
    
}
